//
//  TradeListFilterAlertVM.swift
//  kxf
//
//  Created by Mac on 2024/3/20.
//

import Foundation
import UIKit
import MCToast

class TradeListFilterAlertVM: UIView {
    
    var queryBlock:(()->())?
    
    var payTypeString = ""//2 微信  3  支付宝  4  云闪付
    var statusString = ""//0  成功   3  未支付
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = WHColorWithAlpha(colorStr: "000000", alpha: 0.66)
        self.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(hiddenSelfAciton))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var whiteView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: WHBaseViewVC().getNavigationBarHeight(), width: SCREEN_WIDHT, height: kFitWidth(495)))
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(nothingToDoAction))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    lazy var timeTitleLabel : UILabel = {
        let lab = UILabel.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(12), width: kFitWidth(100), height: kFitWidth(22)))
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.text = "选择日期"
        
        return lab
    }()
    lazy var startTimeBtn : TradeListAlertTimeButton = {
        let btn = TradeListAlertTimeButton.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(46), width: kFitWidth(152), height: kFitWidth(48)))
//        btn.timeLabel.text = "开始时间"
        btn.timeLabel.text = Date().getLast30Days()
        btn.timeString = Date().getLast30Days()
        return btn
    }()
    lazy var endTimeBtn : TradeListAlertTimeButton = {
        let btn = TradeListAlertTimeButton.init(frame: CGRect.init(x: kFitWidth(207), y: kFitWidth(46), width: kFitWidth(152), height: kFitWidth(48)))
//        btn.timeLabel.text = "结束时间"
        return btn
    }()
    lazy var payTypeTitleLabel : UILabel = {
        let lab = UILabel.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(113), width: kFitWidth(100), height: kFitWidth(22)))
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.text = "支付类型"
        
        return lab
    }()
    lazy var weChatBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(21), y: kFitWidth(147), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("微信", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        btn.addTarget(self, action: #selector(wechatTapAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var alipayBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(137), y: kFitWidth(147), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("支付宝", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        btn.addTarget(self, action: #selector(alipayTapAction), for: .touchUpInside)
        return btn
    }()
    lazy var unionPayBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(253), y: kFitWidth(147), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("云闪付", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        btn.addTarget(self, action: #selector(unionTapAction), for: .touchUpInside)
        return btn
    }()
    lazy var sxyPayBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(21), y: kFitWidth(205), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("首信易钱包", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        btn.addTarget(self, action: #selector(sxyTapAction), for: .touchUpInside)
        return btn
    }()
    lazy var payStatusTitleLabel : UILabel = {
        let lab = UILabel.init(frame: CGRect.init(x: kFitWidth(16), y: kFitWidth(272), width: kFitWidth(100), height: kFitWidth(22)))
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        lab.text = "支付状态"
        
        return lab
    }()
    lazy var successBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(21), y: kFitWidth(306), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("成功", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        btn.addTarget(self, action: #selector(successStatusTapAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var notPayBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(137), y: kFitWidth(306), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("未支付", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        btn.addTarget(self, action: #selector(notPayStatusTapAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var refundSuccessBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(253), y: kFitWidth(306), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("退款成功", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        btn.addTarget(self, action: #selector(refundStatusTapAction), for: .touchUpInside)
        
        return btn
    }()
    
    lazy var resetBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(58), y: kFitWidth(391), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("重置", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = WHColor_16(colorStr: "F4F4F4")
        btn.addTarget(self, action: #selector(resetAction), for: .touchUpInside)
        return btn
    }()
    lazy var confirmBtn : UIButton = {
        let btn = UIButton.init(frame: CGRect.init(x: kFitWidth(198), y: kFitWidth(391), width: kFitWidth(101), height: kFitWidth(48)))
        btn.setTitle("确定", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.backgroundColor = .THEME
        
        btn.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        return btn
    }()
}

extension TradeListFilterAlertVM{
    @objc func resetAction() {
        setTypeBtnNormal()
        setStatusBtnNormal()
        startTimeBtn.timeLabel.text = "\(Date().todayDate)"
        endTimeBtn.timeLabel.text = "\(Date().todayDate)"
        startTimeBtn.timeString = "\(Date().todayDate)"
        endTimeBtn.timeString = "\(Date().todayDate)"
    }
    @objc func confirmAction(){
        if !Date().judgeDateIsMin(minDate: startTimeBtn.timeString, maxDate: endTimeBtn.timeString){
            MCToast.mc_text("开始日期不能大于结束日期")
            return
        }
        
        if self.queryBlock != nil{
            self.queryBlock!()
        }
        self.isHidden = true
    }
    @objc func nothingToDoAction() {
        
    }
    @objc func hiddenSelfAciton() {
        self.isHidden = true
    }
    @objc func wechatTapAction() {
        setTypeBtnNormal()
        payTypeString = "2"
        weChatBtn.isSelected = true
        alipayBtn.isSelected = false
        unionPayBtn.isSelected = false
        sxyPayBtn.isSelected = false
        
        weChatBtn.backgroundColor = .THEME
    }
    @objc func alipayTapAction() {
        setTypeBtnNormal()
        payTypeString = "3"
        weChatBtn.isSelected = false
        alipayBtn.isSelected = true
        unionPayBtn.isSelected = false
        sxyPayBtn.isSelected = false
        alipayBtn.backgroundColor = .THEME
    }
    @objc func unionTapAction() {
        setTypeBtnNormal()
        payTypeString = "4"
        weChatBtn.isSelected = false
        alipayBtn.isSelected = false
        unionPayBtn.isSelected = true
        sxyPayBtn.isSelected = false
        unionPayBtn.backgroundColor = .THEME
    }
    @objc func sxyTapAction() {
        setTypeBtnNormal()
        payTypeString = "1"
        weChatBtn.isSelected = false
        alipayBtn.isSelected = false
        unionPayBtn.isSelected = false
        sxyPayBtn.isSelected = true
        sxyPayBtn.backgroundColor = .THEME
    }
    func setTypeBtnNormal() {
        weChatBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        alipayBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        unionPayBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        sxyPayBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        weChatBtn.isSelected = false
        alipayBtn.isSelected = false
        unionPayBtn.isSelected = false
        sxyPayBtn.isSelected = false
        payTypeString = ""
    }
    @objc func successStatusTapAction(){
        setStatusBtnNormal()
        statusString = "0"
        successBtn.isSelected = true
        notPayBtn.isSelected = false
        refundSuccessBtn.isSelected = false
        successBtn.backgroundColor = .THEME
    }
    @objc func notPayStatusTapAction(){
        setStatusBtnNormal()
        statusString = "3"
        successBtn.isSelected = false
        notPayBtn.isSelected = true
        refundSuccessBtn.isSelected = false
        notPayBtn.backgroundColor = .THEME
    }
    @objc func refundStatusTapAction(){
        setStatusBtnNormal()
        statusString = "5"
        successBtn.isSelected = false
        notPayBtn.isSelected = false
        refundSuccessBtn.isSelected = true
        refundSuccessBtn.backgroundColor = .THEME
    }
    func setStatusBtnNormal() {
        statusString = ""
        successBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        notPayBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        refundSuccessBtn.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        successBtn.isSelected = false
        notPayBtn.isSelected = false
        refundSuccessBtn.isSelected = false
    }
}

extension TradeListFilterAlertVM{
    func initUI() {
        addSubview(whiteView)
        whiteView.addSubview(timeTitleLabel)
        whiteView.addSubview(startTimeBtn)
        whiteView.addSubview(endTimeBtn)
        whiteView.addSubview(payTypeTitleLabel)
        whiteView.addSubview(weChatBtn)
        whiteView.addSubview(alipayBtn)
        whiteView.addSubview(unionPayBtn)
        whiteView.addSubview(sxyPayBtn)
        whiteView.addSubview(payStatusTitleLabel)
        whiteView.addSubview(successBtn)
        whiteView.addSubview(notPayBtn)
        whiteView.addSubview(refundSuccessBtn)
        whiteView.addSubview(resetBtn)
        whiteView.addSubview(confirmBtn)
        
        
//        B70
    }
}
